package com.ddwl.user.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.util.RandomUtil;
import com.ddwl.common.constant.RedisKey;
import com.ddwl.common.service.RedisService;
import com.ddwl.schema.vo.user.CaptchaVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author Fu Zhaohui
 * @date 2017/1/3
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CaptchaService {

    private final RedisService redisService;

    public CaptchaVo getCode() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 5, 20);
        String uuid = RandomUtil.randomString(20);
        redisService.getValueOps().set(RedisKey.IMG_CODE_KEY.getKey(uuid), captcha.getCode(), 1, TimeUnit.MINUTES);
        return new CaptchaVo(uuid, captcha.getImageBase64Data());
    }
}
